package com.chuantou.yuba.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.WeatherTO;
import com.chuantou.yuba.util.DateUtil;

public class WeatherPopup extends PopupWindow {
	private ImageView mWeatherImageView;
	private TextView mTempView;
	private TextView mTimeView;
	private TextView mCityView;
	private TextView mPressureView;
	private TextView mWindView;
	private Activity mActivity;
	private Context mContext;
	private LayoutInflater mInflater;

	public WeatherPopup(Activity activity) {
		mActivity = activity;
		mContext = activity.getApplicationContext();
		mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = mInflater.inflate(R.layout.weather, null);
		mWeatherImageView = (ImageView) view.findViewById(R.id.weather_image);
		mTempView = (TextView) view.findViewById(R.id.temp);
		mTimeView = (TextView) view.findViewById(R.id.time);
		mCityView = (TextView) view.findViewById(R.id.city);
		mWindView = (TextView) view.findViewById(R.id.wind);
		mPressureView = (TextView) view.findViewById(R.id.pressure);
		setContentView(view);
		setHeight(FrameLayout.LayoutParams.WRAP_CONTENT);
		setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
		setAnimationStyle(R.style.popup_style);
		ColorDrawable drawable = new ColorDrawable(Color.TRANSPARENT);
		setBackgroundDrawable(drawable);
		setFocusable(true);
		setOutsideTouchable(false);
	}

	public void setWeather(WeatherTO weatherTO) {
		if (weatherTO != null) {
			mCityView.setText(weatherTO.getCityName());
			mTempView.setText(weatherTO.getHighTemp().concat("°C/").concat(weatherTO.getLowTemp()).concat("°C"));
			mTimeView.setText(DateUtil.getDateString(System.currentTimeMillis(), DateUtil.PATTERN_YYYYMMDD).concat("  ")
					.concat(DateUtil.getWeekString(mActivity)));
			mWeatherImageView.setImageResource(getWeatherImage(mContext, weatherTO.getWeatherCode()));
			mCityView.setText(weatherTO.getCityName());
			String wind = mContext.getString(R.string.wind_format, weatherTO.getWindDirection());
			String weather = weatherTO.getWeatherText();
			mWindView.setText(weather.concat(" ").concat(wind));

			String pressure = weatherTO.getPressure();
			if (!TextUtils.isEmpty(pressure)) {
				pressure = mContext.getString(R.string.pressure_format, pressure);
				mPressureView.setText(pressure);
			}
		}
	}

	public int getWeatherImage(Context context, String weatherCode) {
		return context.getResources().getIdentifier("w" + weatherCode, "drawable", context.getPackageName());
	}
}
